﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tracy.DataModel.Config;
using SharedLib.WinFormMVCFramework;
using SharedLib;
using Tracy.Plugins.DownloadAdapters.Model;

namespace Tracy.Plugins.DownloadAdapters.View
{
    public partial class FormUTorrentConfiguration : WinFormViewBase, IConfigView, IFormView
    {
        public event EventHandler<DataChangedEventArgs<IConfigModel>> ConfigChanging;
        public event EventHandler<GenericEventArgs<IConfigModel>> ConfigTesting;
        public FormUTorrentConfiguration()
        {
            InitializeComponent();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        UTorrentConfiguration _currentConfig = null;
        public void LoadConfig(IConfigModel config)
        {
            UTorrentConfiguration utConfig = config as UTorrentConfiguration;
            if (utConfig != null)
            {
                textBoxUserName.Text = utConfig.UserName;
                maskedTextBoxPassword.Text = utConfig.Password;
                textBoxPort.Text = utConfig.Port;
                _currentConfig = utConfig;
            }
        }

        private bool VerifyPort()
        {
            int portNum;
            if (Int32.TryParse(textBoxPort.Text, out portNum))
            {
                if (portNum < 0 || portNum > 65535)
                {
                    MessageBox.Show(TracyFacade.Instance.LanguageManager.GetText("uTorrentAdapter_MessageBox_PortOutOfRange_Message"));
                    return false;
                }
            }
            else
            {
                MessageBox.Show(TracyFacade.Instance.LanguageManager.GetText("uTorrentAdapter_MessageBox_PortIsNotNumber_Message"));
                return false;
            }
            return true;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (VerifyPort())
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
                UTorrentConfiguration config = BuildConfig();
                ConfigChanging.InvokeEvent(this, new DataChangedEventArgs<IConfigModel> (_currentConfig, config));
            }
        }

        private UTorrentConfiguration BuildConfig()
        {
            UTorrentConfiguration config;
            if (_currentConfig != null)
                config = _currentConfig.Clone() as UTorrentConfiguration;
            else
                config = new UTorrentConfiguration();

            config.UserName = textBoxUserName.Text;
            config.Password = maskedTextBoxPassword.Text;
            config.Port = textBoxPort.Text;
            return config;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            UTorrentConfiguration config = BuildConfig();
            ConfigTesting.InvokeEvent(this, new GenericEventArgs<IConfigModel>(config));
        }



    }
}
